package com.net.oil.car;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.net.oil.R;
import com.net.oil.base.CarTypeMessage;
import com.net.oil.car.bean.CarTypeBean;
import com.net.oil.http.AppUrl;
import com.net.oil.util.AlphabetReplaceMethod;
import com.net.oil.util.DisplayUtil;
import com.net.oil.util.glide.GlideUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * 添加车牌号
 */
public class AddCarNumActivity extends AppCompatActivity {


    public static final String CAR_TYPE_BEAN = "car_type_bean";
    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.car_logo)
    ImageView carLogo;
    @BindView(R.id.car_name)
    TextView carName;
    @BindView(R.id.car_type)
    TextView carType;
    @BindView(R.id.save_btn)
    Button saveBtn;
    @BindView(R.id.car_num_et)
    EditText carNumEt;

    private CarTypeBean carTypeBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_car_num);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        toolbarTitle.setText("添加爱车");
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            carTypeBean = (CarTypeBean) bundle.get(CAR_TYPE_BEAN);
        }

        if (carTypeBean != null) {
            carName.setText(carTypeBean.getBrand());
            carType.setText(carTypeBean.getType());
            GlideUtil.getInstance().loadCircleImage(this, carLogo, AppUrl.getImageUrl(carTypeBean.getBrandLogo()), R.drawable.car_logo_err);
        }

        carNumEt.setTransformationMethod(new AlphabetReplaceMethod());

        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveCar();
            }
        });

        toolbarGoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initSpinner();

    }

    private void saveCar() {
        String carNum = spinner.getSelectedItem().toString() + carNumEt.getText().toString().toUpperCase();
        if (TextUtils.isEmpty(carNum)) {
            Toast.makeText(this, "请输入车牌号", Toast.LENGTH_SHORT).show();
            return;
        }
        EventBus.getDefault().post(new CarTypeMessage(carTypeBean.getId(), carNum));     //选择车型后，发送消息
        finish();
    }


    public void initSpinner(){
        spinner.setDropDownWidth(DisplayUtil.dip2px(100));
        spinner.setDropDownVerticalOffset(DisplayUtil.dip2px(50));
        String[] spinnerItems = getResources().getStringArray(R.array.district_list);
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(AddCarNumActivity.this,
                R.layout.item_select, spinnerItems);
        spinner.setAdapter(spinnerAdapter);
    }
}
